package com.canbot.u05.activity.reception;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.canbot.u05.R;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.utils.Logger;
import com.canbot.u05.utils.OkHttpUtils;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.view.UsherModeRecordDialog;

import java.io.File;
import java.util.HashMap;

/**
 * 迎宾模式主界面
 * 迎宾首页。
 */
public class ReceptionModeSpeakToVoiceActivity extends ReceptionBaseActivity implements View.OnClickListener {

        public final String TAG = " UsherModeActivity";

        private UsherModeRecordDialog usherModeRecordDialog;

        private String fileName;

        private ToastTools toastTools;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_reception_mode_speack_voice);
                toastTools = ToastTools.newInstance(this);
        }


        @Override
        protected void onResume() {
                super.onResume();
                if (usherModeRecordDialog == null) {
                        //点击语音录入图标
                        usherModeRecordDialog = new UsherModeRecordDialog(this, UsherModeRecordDialog.USHER_MODE);
                        usherModeRecordDialog.setFinishClickListener(this);
                        usherModeRecordDialog.setCancleClickListener(this);
                        setDialogWindowAttr(usherModeRecordDialog, this);
                        usherModeRecordDialog.show();
                }
        }

        @Override
        public void onClick(View v) {
                switch (v.getId()) {
                        case R.id.record_dialog_finish:
                                fileName = usherModeRecordDialog.ok();
                                Logger.e("test", "fileName" + fileName);
                                usherModeRecordDialog.setTime(ReceptionModeSpeakToVoiceActivity.this.getString(R.string.uploading_please_wait));
                                updataFile();
                                break;
                        case R.id.record_dialog_cancle:
                                usherModeRecordDialog.no();
                                finish();
                                break;
                }
        }

        public static void setDialogWindowAttr(Dialog dlg, Context ctx) {
                dlg.setCancelable(false);
                Window window = dlg.getWindow();
                WindowManager.LayoutParams lp = window.getAttributes();
                lp.gravity = Gravity.CENTER;
                lp.width = (int) ctx.getResources().getDimension(R.dimen.x848);//宽高可设置具体大小
                lp.height = (int) ctx.getResources().getDimension(R.dimen.x499);
                dlg.getWindow().setAttributes(lp);
        }

        private String url = IpMsgConst.HTTP_URL + "meetingaction";

        private void updataFile() {
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("audiofile", new File(UsherModeRecordDialog.RECORD_FILE_DIR, fileName));
                OkHttpUtils.upLoadFile(url, hashMap, new OkHttpUtils.OkHttpCallBack() {
                        @Override
                        public void onSuccess(String result) {
                                Logger.e("test", result + "上传成功");
                                usherModeRecordDialog.cancel();
                                toastTools.showText(getString(R.string.upload_complete));
                                Intent intent = new Intent();
                                intent.putExtra("voiceType", 3);
                                intent.putExtra("voiceContent", "/mnt/internal_sd/ai/ai05res/a1/res/audio/usherVoice/recorder/meeting_recorder.3gp");
                                setResult(RESULT_OK, intent);
                                finish();
                        }

                        @Override
                        public void onFailure() {
                                usherModeRecordDialog.cancel();
                                toastTools.showText(getString(R.string.upload_failed));
                                finish();
                        }
                });
        }
}
